﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RPGServer.Contracts.Requests
{
    static class RequestGOLIMR
    {
        /// <summary>
        /// 处理登录操作
        /// </summary>
        public static void Process(RPGPlayer player, byte[] buffer, int length)
        {
            int offset = 2;
            uint playerId = IntUtil.UIntFromBuffer(buffer, ref offset);
            uint objectId = IntUtil.UIntFromBuffer(buffer, ref offset);
            ushort mapId = IntUtil.UShortFromBuffer(buffer, ref offset);
            ushort posRow = IntUtil.UShortFromBuffer(buffer, ref offset);
            ushort posCol = IntUtil.UShortFromBuffer(buffer, ref offset);
            ushort rangeRow = IntUtil.getByte(buffer, ref offset);
            ushort rangeCol = IntUtil.getByte(buffer, ref offset);
            ushort segementId = IntUtil.getByte(buffer, ref offset);

            Log.Debug("request", "玩家({0})请求获取map:{1}上的单位信息",
                playerId,
                mapId);

            // 验证用户是否符合要求
            RPGMapPlayerCollection map = CacheData.GetInstance().GetMap(mapId);
            if (map != null)
            {
                int objectCount = map.PlayerList.Count;
                byte[] data = new byte[(objectCount + 1)* 29];
                offset = 0;
                // scenario
                IntUtil.ByteToBuffer(RPGNET_SCENARIO.GAME, data, offset, out offset);
                // cmd
                IntUtil.ByteToBuffer(RPGNET_RESPONSE.GOLIMR, data, offset, out offset);
                // error code
                IntUtil.ByteToBuffer(RPGNET_RESPONSE_ERROR.NOERROR, data, offset, out offset);
                // mapId
                IntUtil.SetUShort(map.MapId, data, ref offset);
                // object Total Count
                IntUtil.SetUShort((ushort)objectCount, data, ref offset);
                // object count
                IntUtil.SetUShort((ushort)objectCount, data, ref offset);

                for (int objIndex = 0; objIndex < objectCount && objIndex < map.PlayerList.Count; objIndex++)
                {
                    RPGPlayer objItem = map.PlayerList[objIndex];
                    if (objItem != null)
                    {
                        // player id
                        IntUtil.SetUInt(objItem.Id, data, ref offset);
                        // object id
                        IntUtil.SetUInt(objItem.RoleId, data, ref offset);
                        // object name
                        IntUtil.SetString(String.Format("object-{0}", objItem.RoleId), data, ref offset, 16);
                        // model id
                        IntUtil.SetUShort(objItem.RoleModelId, data, ref offset);
                        // pos row
                        IntUtil.SetUShort(objItem.CurPosRow, data, ref offset);
                        // pos col
                        IntUtil.SetUShort(objItem.CurPosCol, data, ref offset);
                    }
                    else
                    {
                        // player id
                        IntUtil.SetUInt(0, data, ref offset);
                        // object id
                        IntUtil.SetUInt(0, data, ref offset);
                        // object name
                        IntUtil.SetString(string.Empty, data, ref offset, 16);
                        // model id
                        IntUtil.SetUShort(0, data, ref offset);
                        // pos row
                        IntUtil.SetUShort(0, data, ref offset);
                        // pos col
                        IntUtil.SetUShort(0, data, ref offset);
                    }
                }

                // 告诉玩家登录成功
                player.Client.SendData(data, offset);
                Log.Debug("request", "玩家({0})请求获取单位数据成功,map:{1} objectcount:{2}",
                    playerId,
                    mapId,
                    objectCount);
            }
            else
            {
                byte[] data = new byte[255];
                offset = 0;
                // scenario
                IntUtil.ByteToBuffer(RPGNET_SCENARIO.INIT, data, offset, out offset);
                // cmd
                IntUtil.ByteToBuffer(RPGNET_RESPONSE.GOLIMR, data, offset, out offset);
                // error code
                IntUtil.ByteToBuffer(RPGNET_RESPONSE_ERROR.UNKNOWN, data, offset, out offset);

                player.Client.SendData(data, offset);
                Log.Debug("response", "玩家({0})获取单位信息失败!", playerId);
            }
        }
    }
}
